#include<SDL.h>
#include "colision.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_mixer.h"


int jeu(SDL_Surface *screen)
{int i,j;
  int tempsPrecedent = 0, tempsActuel = 0;
    SDL_Event event;
    SDL_Rect dstrect, dst,dt;    // initialize SDL video
    int done=0;



    // load an image

    SDL_Surface* bmp1 = SDL_LoadBMP("img/f1.bmp");
    SDL_Surface* bmp2 = SDL_LoadBMP("img/f1.bmp");
    SDL_Surface* bmp3 = SDL_LoadBMP("img/f1.bmp");
    SDL_Surface* bmp4 = SDL_LoadBMP("img/f1.bmp");
    SDL_Surface* bmp5 = SDL_LoadBMP("img/f1.bmp");
    SDL_Surface* bmp6 = SDL_LoadBMP("img/f1.bmp");

    SDL_Surface *image1 = SDL_LoadBMP("img/mario.bmp");

      SDL_Surface *image2 = SDL_LoadBMP("img/11.bmp");

     SDL_Surface* im2 = SDL_LoadBMP("img/image2.bmp");

    SDL_Surface* im3 = SDL_LoadBMP("img/plateforme.bmp");

    SDL_Surface* arg = SDL_LoadBMP("img/arg.bmp");

    dst.x=250;
    dst.y=350;
    dstrect.x =0 ;
    dstrect.y = 490;
    int x= image1->w +dstrect.x;
    int y= image1->h +dstrect.y;
     dt.x=50;
    dt.y=520;

    // program main loop

    SDL_EnableKeyRepeat(10,10);
    SDL_SetColorKey(image1,SDL_SRCCOLORKEY,SDL_MapRGB(image1->format,255,255,255));
    SDL_SetColorKey(image2,SDL_SRCCOLORKEY,SDL_MapRGB(image2->format,255,255,255));
        SDL_SetColorKey(arg,SDL_SRCCOLORKEY,SDL_MapRGB(arg->format,255,255,255));





    while (!done)
    {

        while (SDL_PollEvent(&event))
        {
            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                done = 1;
                break;
                // check for keypresses
            case SDL_KEYDOWN:
            {
                // exit if ESCAPE is pressed
                switch (event.key.keysym.sym)
                {
                case  SDLK_ESCAPE:
                  return(1);
                    break;
                case SDLK_SPACE:
                if (collisionBackground( bmp1, dstrect.x+36, dstrect.y)==0)

                    dstrect.y=210 ;
                    break;
                case SDLK_RIGHT:
                    i=0;
                if (collisionBackground( bmp1, dstrect.x+73, dstrect.y+45)==0)

                    dstrect.x=dstrect.x+5;
                     j=j+5;
                    break;
                case SDLK_LEFT:
                    i=1;
                if (collisionBackground( bmp1, dstrect.x, dstrect.y+45)==0)
                    dstrect.x=dstrect.x-5;
                    j=j-5;

                    break;
                }}break;
                case SDL_KEYUP:
                {
                    switch (event.key.keysym.sym)
                    case SDLK_SPACE:
                    if(collisionBackground( bmp1, dstrect.x+36, dstrect.y+92)==0)

                    dstrect.y=dstrect.y+280;
                    break;
                }break;
            }}




    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
    if((dstrect.x)>0)
    SDL_BlitSurface(bmp1, NULL, screen, NULL);
    if((dstrect.x)>150)
    SDL_BlitSurface(bmp2, NULL, screen, NULL);
    if((dstrect.x)>300)
    SDL_BlitSurface(bmp3, NULL, screen, NULL);
    if((dstrect.x)>450)
    SDL_BlitSurface(bmp4, NULL, screen, NULL);
    if((dstrect.x)>600)
    SDL_BlitSurface(bmp5, NULL, screen, NULL);
    if(dstrect.x==750)
    SDL_BlitSurface(bmp6, NULL, screen, NULL);

    SDL_BlitSurface(bmp1, NULL, screen, NULL);

        SDL_BlitSurface(im3, NULL, screen, &dst);
               SDL_BlitSurface(arg, NULL, screen, &dt);

   if(i==0)

   SDL_BlitSurface(image1, NULL, screen, &dstrect);


   if(i==1)

     SDL_BlitSurface(image2, NULL, screen, &dstrect);


    SDL_Flip(screen);
    }



SDL_FreeSurface(bmp1);
SDL_FreeSurface(bmp2);
SDL_FreeSurface(bmp3);
SDL_FreeSurface(bmp4);
SDL_FreeSurface(bmp5);
SDL_FreeSurface(bmp6);

SDL_FreeSurface(image1);
SDL_FreeSurface(image2);
SDL_FreeSurface(screen);



}



